package cn.hl.hm.util;

import java.io.File;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;

import cn.hl.hm.constants.UploadConfig;

@Controller
public class OSSUtil {
	@Autowired
	@Qualifier("uploadConfig")
	private UploadConfig uploadConfig;
	
	public boolean putObject(String string, InputStream inputStream) {
		// endpoint以杭州为例，其它region请按实际情况填写
		String endpoint = uploadConfig.getEndpoint();
		// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建
		String accessKeyId = uploadConfig.getAccessKeyId();
		String accessKeySecret = uploadConfig.getAccessKeySecret();
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 上传文件
		ossClient.putObject(uploadConfig.getBucketName(), string, inputStream);
		// 关闭client
		ossClient.shutdown();
		return true;
	}
	
	
	public boolean putObject(String string, File file) {
		// endpoint以杭州为例，其它region请按实际情况填写
		String endpoint = uploadConfig.getEndpoint();
		// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建
		String accessKeyId = uploadConfig.getAccessKeyId();
		String accessKeySecret = uploadConfig.getAccessKeySecret();
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 上传文件
		ossClient.putObject(uploadConfig.getBucketName(), string, file);
		// 关闭client
		ossClient.shutdown();
		return true;
	}

}
